<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Selfhelp\Controller;
use Think\Controller;

class UploadController extends Controller {
	
	public function index(){
		$this->display();
	}

	public function indextest(){
		$this->display();
	}

	/*
	 * 保存上传资料
	 */
	public function add(){
		$rules = array(
				array('videourl','require','请上传视频！'),
				array('title','require','视频标题不能为空！'),
				array('des','require','视频介绍不能为空！'),
		);
		$resource = D(C('TABLE_SELFHELPRESOURCE'));
		if(!$resource->validate($rules)->create()){
			$this->error($resource->getError(),'',1);
		}else{
			$resource->user_token = $_COOKIE['user_userinfo'];
			$resource->cdate = date("Y-m-d H:i:m");
			$result = $resource->add();
			if($result){
				$this->success('添加成功','/Selfhelp/Index/lists');
			}else{
				$this->error('添加失败','',1);
			}
		}
	}

	/*
	 * 修改资料
	 */
	public function edit(){
		$id = I('get.id');
		if(!empty($id)){
			$rules = array(
					array('videourl','require','请上传视频！'),
					array('title','require','视频标题不能为空！'),
					//array('des','require','视频介绍不能为空！'),
			);
			$resource = D(C('TABLE_SELFHELPRESOURCE'));
			if(!$resource->validate($rules)->create()){
				$this->error($resource->getError(),I('post.weburl'),1);
			}else{
				$data['title'] = I('post.title');
				$data['videourl'] = I('post.videourl');
				$data['des'] = I('post.des');
				$data['shoplink'] = I('post.shoplink');
				$data['imgurl'] = I('post.imgurl');
				$data['eplink'] = I('post.eplink');
				if(!empty(I('post.tel'))){
					$tel = I('post.tel');
				}else{
					$tel = null;
				}
				$data['tel'] = $tel;
				$result = $resource->where(array('id'=>$id))->save($data);
				if($result){
					$data1['status'] = 1; //1审核 2通过
					$result = $resource->where(array('id'=>$id))->save($data1);
					if($result){
						$this->success('修改成功','/Selfhelp/Index/lists');
					}else if($result==0){
						header('Location:/Selfhelp/Index/lists');
					}else{
						$this->error('权限更新失败','',1);
					}
				}else if($result==0){
					header('Location:/Selfhelp/Index/lists');
				}else{
					$this->error('修改失败','',1);
				}
			}
		}else{
			$this->error('参数错误','',1);
		}

	}

	/*
	 * 修改页
	 */
	public function editinfo(){
		$id = I('get.id');
		if(!empty($id)){
			$resource = D(C('TABLE_SELFHELPRESOURCE'));
			$res = $resource->find($id);
			if($res){
				$this->assign('signPackage',$this->weixinshare());
				$this->assign('weburl',getURL());
				$this->assign('info',$res);
				$this->display();
			}else{
				$this->error('空数据','',2);
			}
		}else{
			$this->error('参数错误','',2);
		}
	}
	protected function weixinshare(){
		include('jssdk.php');
		$jssdk = new \JSSDK("wx30934416968cbcb7", "2a1dabeb57ac5688b33117d29f6ec062");
		$signPackage = $jssdk->GetSignPackage();
		return $signPackage;
		//$this->assign('signPackage',$signPackage);
	}
	
	/*
	 * 视频上传
	 */
	public function up(){
		$size = 31457280;
		$exts = array('mp4', 'mov', 'ogg');
		$rootpath = './Uploadmp4/';
		$upload = new \Think\Upload();			//实例化上传类
		$upload->maxSize   =     $size;			//设置附件上传大小30M
		$upload->exts      =     $exts;	//设置附件上传类型
		$upload->rootPath  =     $rootpath; 	//设置附件上传根目录
		// 上传单个文件
		$info   =   $_FILES['myfiles'];
		if($info['type']!='video/mp4' && $info['type']!='video/mov' && $info['type']!='video/ogg' && $info['type']!='video/quicktime'){
			//echo $info['type'];
			echo 1;//不符合上传文件类型
		}else{
			$info1   =   $upload->uploadOne($info);
			if(is_array($info1)){
				if($info1['size']>$size){
					echo 2;//超出限定范围30M
				}else if(empty($info1['name'])){
					echo 3;//没有上传文件
				}else{
					//echo C('WEBDOMAIN').'/'.$info['savepath'].$info['savename']; //返回路径
					echo ltrim($rootpath.$info1['savepath'].$info1['savename'],'.');
				}
			}else{
				echo 0;//上传失败
			}
		}

	}

	/*
	 * 图片上传
	 */
	public function imgup(){
		$size = 6291456;
		$exts = array('jpg', 'jpeg', 'gif', 'png');
		$rootpath = './Uploadmp4/';
		$upload = new \Think\Upload();			//实例化上传类
		$upload->maxSize   =     $size;			//设置附件上传大小3M
		$upload->exts      =     $exts;	//设置附件上传类型
		$upload->rootPath  =     $rootpath; 	//设置附件上传根目录
		// 上传单个文件
		$info   =   $_FILES['myimgfiles'];
		if($info['type']!='image/gif' && $info['type']!='image/jpg' && $info['type']!='image/png' && $info['type']!='image/jpeg'){
			//echo $info['type'];
			echo 1;//不符合上传文件类型
		}else{
			$info1   =   $upload->uploadOne($info);
			if(is_array($info1)){
				if($info1['size']>$size){
					echo 2;//超出限定范围6M
				}else if(empty($info1['name'])){
					echo 3;//没有上传文件
				}else{
					//echo C('WEBDOMAIN').'/'.$info['savepath'].$info['savename']; //返回路径
					echo ltrim($rootpath.$info1['savepath'].$info1['savename'],'.');
				}
			}else{
				echo 0;//上传失败
			}
		}

	}

	public function testindex(){
		$this->display();
	}

	public function savimgtonew(){
		$datastr = I('post.imgdata');
		if(!empty($datastr)){
			$s = base64_decode(str_replace('data:image/png;base64,', '', $datastr));
			$rootpath = './IndexPublic/selfhelp/changeimg/'.date('Y-m-d',time()).'/'; //保存路径
			if (!is_dir($rootpath)) mkdir($rootpath);
			$saveimgfile = $rootpath.uniqid().'.jpg';
			$imgtosave = file_put_contents($saveimgfile, $s);
			if($imgtosave){
				echo 'http://h5.hrtv.cn'.ltrim($saveimgfile,'.');
			}else{
				echo 0;
			}
		}else{
			echo 0;//无效
		}

	}

}